package io.seata.samples.integration.order.service;

import java.util.HashMap;
import java.util.UUID;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.protobuf.Empty;
import io.seata.samples.integration.common.account.AccountLeftResponse;
import io.seata.samples.integration.common.account.AccountRequest;
import io.seata.samples.integration.common.account.AccountResponse;
import io.seata.samples.integration.common.account.GreeterGrpc;
import io.seata.samples.integration.common.dto.OrderDTO;
import io.seata.samples.integration.common.enums.RspStatusEnum;
import io.seata.samples.integration.common.response.ObjectResponse;
import io.seata.samples.integration.order.entity.TOrder;
import io.seata.samples.integration.order.mapper.TOrderMapper;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * * @author lidong
 * @since 2019-09-04
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {
    @GrpcClient("account-grpc-server")
    private GreeterGrpc.GreeterBlockingStub stub;

    /**
     * 创建订单
     * @Param:  OrderDTO  订单对象
     * @Return:  OrderDTO  订单对象
     */
    @Override
    public ObjectResponse<OrderDTO> createOrder(OrderDTO orderDTO) {
        ObjectResponse<OrderDTO> response = new ObjectResponse<>();
        //扣减用户账户
        /*AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(orderDTO.getUserId());
        accountDTO.setAmount(orderDTO.getOrderAmount());*/
        AccountRequest accountDTO = AccountRequest.newBuilder()
                .setUserId(orderDTO.getUserId())
                .setAmount(orderDTO.getOrderAmount().doubleValue())
                .build();
        AccountResponse accountResponse = stub.decreaseAccount(accountDTO);

        //生成订单号
        orderDTO.setOrderNo(UUID.randomUUID().toString().replace("-",""));
        //生成订单
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(orderDTO,tOrder);
        tOrder.setCount(orderDTO.getOrderCount());
        tOrder.setAmount(orderDTO.getOrderAmount().doubleValue());
        try {
            baseMapper.createOrder(tOrder);
        } catch (Exception e) {
            response.setStatus(RspStatusEnum.FAIL.getCode());
            response.setMessage(RspStatusEnum.FAIL.getMessage());
            return response;
        }

        if (accountResponse.getStatus() != 200) {
            response.setStatus(RspStatusEnum.FAIL.getCode());
            response.setMessage(RspStatusEnum.FAIL.getMessage());
            return response;
        }

        response.setStatus(RspStatusEnum.SUCCESS.getCode());
        response.setMessage(RspStatusEnum.SUCCESS.getMessage());
        return response;
    }

    @Override
    public ObjectResponse getOrderSum() {
        ObjectResponse objectResponse = new ObjectResponse();

        AccountLeftResponse accountLeft = stub.getAccountLeft(Empty.newBuilder().build());

        HashMap<String, Object> data = new HashMap<>();
        data.put("total",baseMapper.getOrderSum());
        data.put("left",accountLeft.getLeft());
        objectResponse.setData(data);
        return objectResponse;
    }
}
